package shuati;

import java.util.*;

/**
 * @author 23050
 * @version 1.0
 * @description: TODO
 * @date 2023/12/17 10:28
 */
public class Demo03 {
/*    public int[] findMissingAndRepeatedValues(int[][] grid) {
        int a = 0;
        int b = 0;
        int n = grid.length;
        int[] arr = new int[n*n+1];
        for(int i = 0;i<n;i++){
            for(int j = 0;j<n;j++){
                arr[grid[i][j]]++;
                if (arr[grid[i][j]] == 2){
                    a = grid[i][j];
                }
            }
        }
        for(int i =1;i<n*n+1;i++){
            if (arr[i]==0){
                b = i;
                break;
            }
        }
        int[] ans = {a,b};
        return ans;

    }*/

/*    public int[][] divideArray(int[] nums, int k) {
        List<int[]> ret = new ArrayList<>();
        if (nums.length <3){
            return new int[0][3];
        }
        Arrays.sort(nums);

        int left = 0;
        int right = 2;
        while (right < nums.length) {
            if (nums[right] - nums[left] <= k ){
                int[] tmp = {nums[left],nums[left+1],nums[left+2]};
                ret.add(tmp);
                right+=3;
                left+=3;
            }else{
                right++;
                left++;
            }
        }
        int[][] result = new int[ret.size()][3];
        for (int i = 0;i<ret.size();i++){
            result[i] = ret.get(i);
        }

        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for(int i = 0;i<nums.length;i++){
            set1.add(nums[i]);
        }
        for(int i = 0;i<result.length;i++){
            for(int j = 0;j<3;j++){
                set2.add(result[i][j]);
            }
        }
        if (set1.size() == set2.size()){
            return result;
        }else{
            return new int[0][3];
        }




    }*/

    public static void main(String[] args) {
        System.out.println(minimumCost(new int[]{102,103,105,106,109}));
        System.out.println();
    }
    public static  long minimumCost(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        int min = nums[0];
        int max = nums[n-1];
        long tmp = Long.MAX_VALUE;
        for(int i = max;i>=min;i--){
            if (isT(i)){
                long sum1 = 0;
                for(int j =0;j<n;j++){
                    sum1+=Math.abs(nums[j]-i);
                }
                tmp = Math.min(tmp,sum1);
            }
        }
        if (tmp!=Long.MAX_VALUE){
            return tmp;
        }
        for(int i = min;i>0;i--){
            if (isT(i)){
                long sum1 = 0;
                for(int j =0;j<n;j++){
                    sum1+=Math.abs(nums[j]-i);
                }
                tmp = Math.min(tmp,sum1);
                break;
            }
        }
        for(int i = max;i<1000000000;i++){
            if (isT(i)){
                long sum1 = 0;
                for(int j =0;j<n;j++){
                    sum1+=Math.abs(nums[j]-i);
                }
                tmp = Math.min(tmp,sum1);
                break;
            }
        }
        return tmp;
    }

    //是不是回文数
    private static boolean isT(int i) {
        if (i<10){
            return true;
        }
        String ret = String.valueOf(i);
        int left = 0;
        int right = ret.length()-1;
        while (left<right){
            if (ret.charAt(left) != ret.charAt(right)){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }


}

























